package com.it.park.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.QuestionAnswerService;
import com.it.park.client.service.QuestionService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.Question;
import com.it.park.model.QuestionAnswer;

@Controller
@RequestMapping("/admin/question")
public class AdminQuestionController {
	private String rootPath=Constant.ADMIN_PATH;

	private Page page = null;

	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionAnswerService questionAnswerService;
	/**
	 * 问答页面
	 * @return
	 */
	@RequestMapping("questionList")
	public String questionList(HttpServletRequest request){
			return rootPath+"question/questionList";
	}
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(HttpServletRequest request){
		Integer pageNow = Integer.parseInt(request.getParameter("page"));
		Integer rows = Integer.parseInt(request.getParameter("rows"));
		Map<String,Object> map_ = new HashMap<String, Object>();
		Integer total = questionService.selectTotal(map_);
		page = new Page(pageNow,total,rows);
		map_.put("startPos", page.getStartPos());
		map_.put("pageSize", page.getPageSize());
		List<Question> list = questionService.selectPagination(map_);
		Map<String,Object> map  =new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	
	

	@RequestMapping("save")
	@ResponseBody
	public JsonObj save(Question question,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = questionService.insertSelective(question);
		if(success){
			json.setMessage("添加成功!");
		}else{
			json.setMessage("添加失败!");
		}
		json.setSuccess(success);
		
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public JsonObj deleteById(@RequestParam("id") int id,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = questionService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
		}else{
			json.setMessage("删除失败!");
		}
		json.setSuccess(success);
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public JsonObj edit(Question question,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = questionService.updateByPrimaryKeySelective(question);
		if(success){
			json.setMessage("修改成功!");
		}else{
			json.setMessage("修改失败!");
		}
		
		json.setSuccess(success);
		return json;
	}
	
	/**
	 * 根据问题ID查看回答
	 */
	@ResponseBody
	@RequestMapping("listCommentsByQuestionId")
	public Map<String,Object> listCommentsByQuestionId(@RequestParam("page")Integer pageNow,Integer questionId,Integer rows){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("questionId", questionId);
		Integer total = questionAnswerService.selectTotal(map);
		page = new Page(pageNow,total,rows);
		map.put("startPos", page.getStartPos());
		map.put("pageSize", page.getPageSize());
		List<QuestionAnswer> questionAnswers=questionAnswerService.selectPagination(map);
		Map<String,Object> _map  =new HashMap<String, Object>();
		_map.put("total", total);
		_map.put("rows", questionAnswers);
		return _map;
	}
	
	
}
